from app import create_app, db
from app.models import User, Post, Comment
from config import Config

app = create_app()
app.config['SQLALCHEMY_DATABASE_URI'] = Config.SQLALCHEMY_DATABASE_URI

with app.app_context():
    # 创建数据库表
    db.create_all()

    # 添加测试数据
    if not User.query.first():
        # 添加测试用户
        user1 = User(username='admin', email='admin@example.com',
                    password='$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW')
        user2 = User(username='user', email='user@example.com',
                    password='$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW')

        db.session.add(user1)
        db.session.add(user2)

        # 添加测试帖子
        post1 = Post(title='First Post', content='This is the first post content', user_id=1)
        post2 = Post(title='Second Post', content='This is the second post content', user_id=2)

        db.session.add(post1)
        db.session.add(post2)

        # 添加测试评论
        comment1 = Comment(content='Great post!', user_id=2, post_id=1)
        comment2 = Comment(content='Thanks for sharing', user_id=1, post_id=2)

        db.session.add(comment1)
        db.session.add(comment2)

        db.session.commit()
        print('Database initialized with test data')
    else:
        print('Database already contains data')